package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.CluePoolDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * ClassName: ClueServiceImpl
 * Package: com.qk.service.impl
 * Description:
 *
 * @Author zt
 * @Create 2025/8/2 16:38
 * @Version 1.0
 */
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }

    /*
    跟进线索
     */
    @Transactional(rollbackFor = Exception.class)   //事务控制  - 默认只有运行时异常和错误才会回滚，这里设置所有异常都回滚
    @Override
    public void trackClue(Clue clue) {
        //1.根据ID更新线索的基本信息
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //添加跟进记录 - 插入一条数据
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());//线索ID
        //clueTrackRecord.setUserId(1);//跟进人ID--->当前登录用户ID ToDO，先写死，后续完毕
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//取出用户id
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    /*
    伪线索
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void falseClue(Clue clue) {
        //1.根据ID更新线索的基本信息
        clue.setStatus(4);//伪线索
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //添加一条跟进记录，跟进类型type为0，表示伪线索
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setType(0);//跟进的类型为0，表示伪线索
        clueTrackRecord.setFalseReason(clue.getFalseReason());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Override
    public void toBusiness(Integer id) {

        //1.根据ID更新线索状态（转商机）
        Clue clue = clueMapper.selectById(id);//根据id找出线索

        clue.setStatus(5);//转商机
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);//更新线索状态

        //2.保存商机信息
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);//拷贝
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId(id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);
    }

    /*
    线索池列表查询
     */

    @Override
    public PageResult<Clue> listCluePool(CluePoolDto cluePoolDto) {
        Page<Clue> page = clueMapper.listCluePool(new Page<>(cluePoolDto.getPage(), cluePoolDto.getPageSize()), cluePoolDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
